﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DAOFileTrans
/// </summary>
public class DAOFileTrans
{
    DBFileTransDataContext db = new DBFileTransDataContext();

	public DAOFileTrans()
	{
		
	}

    #region AuthUser
    /// <summary>
    /// authenticate user and return group ID
    /// </summary>
    /// <param name="username"></param>
    /// <param name="pwd"></param>
    /// <returns></returns>
    public int GetUserAuth(string username, string pwd)
    {
        using (DBFileTransDataContext db = new DBFileTransDataContext())
        {
           // int groupID = Convert.ToInt32(db.authUser(username, pwd));
            int groupID = db.authUser(username, pwd);
            return groupID;
        }
    }

    #endregion

    #region GetList

    public List<FileInformation> GetList(string username, string pwd, int gID)
    {
        using (DBFileTransDataContext db = new DBFileTransDataContext())
        {
            var gFiles = db.GetGroupFiles(username, pwd, gID);

            List<FileInformation> groupFiles = new List<FileInformation>();


            foreach (GetGroupFilesResult gfr in gFiles)
            {
                FileInformation fi = new FileInformation();
                fi.Path = gfr.fPath;
                //fi.FileName = gfr.fShortName;
                fi.GroupID = gfr.gID;
                fi.GroupName = gfr.gName;
                fi.Size = (long) gfr.fileSize;

                groupFiles.Add(fi);
            }
            return groupFiles;
        }
    }
    #endregion

    #region GetFileInformation

    /// <summary>
    /// retrieve file information using filename
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public List<getFileInformationResult> GetFileInformation(string fileName)
    {
        using (DBFileTransDataContext db = new DBFileTransDataContext())
        {
            List<getFileInformationResult> fileInfo = db.getFileInformation(fileName).ToList();
            return fileInfo;
        }
    }
    #endregion

    #region GetGroups
    public List<HelperClass> getGroups()
    {
        var groups = from g in db.vwGroups select g;

        List<HelperClass> hcg = new List<HelperClass>();
        foreach (vwGroup vwg in groups)
        {
            HelperClass hc = new HelperClass();
            hc.GroupID = vwg.gID;
            hc.GroupName = vwg.gName;
            hcg.Add(hc);
        }
        return hcg;
    }

    #endregion

    #region AddUser

    public int addUser(HelperClass user, HelperClass newUser)
    {
        int result = db.addUser(user.UserName, user.PassHash, newUser.UserName, newUser.PassHash, newUser.UserGroup);
        return result;
    }

    #endregion

    #region DeleteUser
    public int DeleteUser(string username, string pwd, string delUserName)
    {
        int result = db.deleteUser(username, pwd, delUserName);
        return result;
        
    }

    #endregion

    #region ChangePassword

    public int ChangePassword(string username, string pwd, string updateUser, string newPwd)
    {
        int result = db.changePassword(username, pwd, updateUser, newPwd);
        return result;
    }


    #endregion
    


    #region AddGroup
    public int AddGroup(string username, string pwd, string groupName)
    {
        int result = db.addGroup(username, pwd, groupName);
        return result;
        
    }

    #endregion

    #region DeleteGroup

    public int DelGroup(string username, string pwd, string groupName)
    {
        int result = db.deleteGroup(username, pwd, groupName);
        return result;
    }

    #endregion


    #region GetUsers
    public List<HelperClass> GetUsers(string username, string pwd, string groupName)
    {
        using (DBFileTransDataContext db = new DBFileTransDataContext())
        {
            var uNames = db.getUsers(username, pwd, groupName);

            List<HelperClass> users = new List<HelperClass>();

            foreach (getUsersResult gur in uNames)
            {
                HelperClass hc = new HelperClass();
                hc.UserName = gur.uLogin;
                users.Add(hc);
            }
            return users;
        }
        
    }

    #endregion

    #region DeleteFiles

    public int DeleteFile(string username, string pwd, string filename)
    {

        int result = db.deleteFile(username, pwd, filename);
        return result;
    }



    #endregion

    #region AddFiles

    public int AddFile(string username, string pwd, string filename, string groupName, long filesize)
    {

        int result = db.addFile(username, pwd, filename, groupName, filesize);
        return result;
    }



    #endregion

    #region GetGroupName

    public  string getUsersGroup(string username, string pwrd)
    {
        return db.getUserGroup(username, pwrd).SingleOrDefault().userGroup;
        
    }


    #endregion





}